package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;

    @PostMapping
    public R saveWithFlavor(@RequestBody DishDto dto){
        log.info("新增菜品dto:{}",dto);
        dishService.saveWithFlavor(dto);
        return R.success("新增菜品成功");
    }
    //菜品分页查询
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        Page<DishDto> pageInfo = dishService.pageByName(page, pageSize, name);
        return R.success(pageInfo);
    }

    //修改菜品信息 --分两步
     //先查出回响到页面
     //修改在保存


    //根据id查询菜品信息和口味
    @GetMapping("/{id}")
    public R getWithFlavor(@PathVariable Long id){
        DishDto dto=dishService.getWithFlavor(id);
        return R.success(dto);
    }
    //根据id修改菜品信息和口味
    @PutMapping
    public R updateWithFlavor(@RequestBody DishDto dto){
        dishService.updateWithFlavor(dto);
        return R.success("修改陈宫");
    }

    //菜品列表的查询
    @GetMapping("/list")
    public R list(Dish dish){
        Long categoryId = dish.getCategoryId();//菜品分类id
        String name = dish.getName();//菜品名称

        //构建条件
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();

        //查询状态为1的数据
        lqw.eq(Dish::getStatus,1);
        //根据菜品名称模糊查询
        lqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //修改时间倒序排序
        lqw.orderByDesc(Dish::getUpdateTime);
        //根据菜品分类id精确查询
        lqw.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        //发起查询
        List<Dish> list = dishService.list(lqw);
        //将List<Dish>转换为List<DishDto>
        List<DishDto> resultList=list.stream().map(dishItem->{
            DishDto dto = new DishDto();
            //将dishItem考呗给dto
            BeanUtils.copyProperties(dishItem,dto);
            //查询当前菜品的口味信息
            LambdaQueryWrapper<DishFlavor> dflqw = new LambdaQueryWrapper<>();
            dflqw.eq(DishFlavor::getDishId,dishItem.getId());
            //口味信息dishFlavorList
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dflqw);
            //把当前的菜品口味信息设置到dto当中
           dto.setFlavors(dishFlavorList);
            return dto;
        }).collect(Collectors.toList());

        return R.success(resultList);
    }

}
